package com.xuecheng.content.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.commons.constants.CourseConstants;
import com.xuecheng.commons.enums.ErrorCode;
import com.xuecheng.commons.model.dto.TeachplanDto;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.model.vo.TeachplanVo;
import com.xuecheng.commons.utils.BeanHelper;
import com.xuecheng.content.domain.CourseBase;
import com.xuecheng.content.domain.Teachplan;
import com.xuecheng.content.mapper.TeachplanMapper;
import com.xuecheng.content.service.CourseBaseService;
import com.xuecheng.content.service.TeachplanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.web.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程计划 服务实现类
 * </p>
 *
 * @author HuangYuwu
 * @since 2022-09-11
 */
@Service
public class TeachplanServiceImpl extends ServiceImpl<TeachplanMapper, Teachplan> implements TeachplanService {

    @Autowired
    private TeachplanMapper teachplanMapper;

    @Autowired
    private CourseBaseService courseBaseService;

    /**
     * 查询课程计划列表-树形结构
     *
     * @param courseId
     * @return
     */
    @Override
    public ResponseResult treeNodes(Long courseId) {
        List<TeachplanVo> teachplanVoList = teachplanMapper.treeNodes(courseId);
        TeachplanVo teachplanOne = teachplanVoList.stream().filter(vo -> vo.getGrade() == 1).findFirst().get();
        List<TeachplanVo> voListTwo = teachplanVoList.stream()
                .filter(vo -> vo.getParentid().equals(teachplanOne.getId()))
                .collect(Collectors.toList());
        if (voListTwo.size() != 0) {
            for (TeachplanVo teachplanVo : voListTwo) {
                List<TeachplanVo> voListTree = teachplanVoList.stream()
                        .filter(vo -> vo.getParentid().equals(teachplanVo.getId()))
                        .collect(Collectors.toList());
                teachplanVo.setTeachPlanTreeNodes(voListTree);
            }
        }
        teachplanOne.setTeachPlanTreeNodes(voListTwo);
        return ResponseResult.okResult(teachplanOne);
    }

    /**
     * 保存或者更新课程计划
     *
     * @param teachplanDto
     * @return
     */
    @Override
    public ResponseResult saveOrUpdateTeachplan(TeachplanDto teachplanDto) {
        CourseBase courseBase = courseBaseService.getById(teachplanDto.getCourseId());
        //判断状态是否为可修改
        String auditStatus = courseBase.getAuditStatus();
        if (StrUtil.equals(auditStatus, CourseConstants.AUDIT_COMMIT_STATUS) ||
                StrUtil.equals(auditStatus, CourseConstants.AUDIT_PASTED_STATUS) ||
                StrUtil.equals(auditStatus, CourseConstants.AUDIT_PUBLISHED_STATUS)) {
            throw new BusinessException(ErrorCode.UPDATEEERROR);
        }
        Teachplan teachplan = BeanHelper.copyProperties(teachplanDto, Teachplan.class);
        //如果新增是第二级章节 则查询对应的父级id
        if (teachplan.getParentid()==null&&teachplan.getGrade() == 2) {
            LambdaQueryWrapper<Teachplan>queryWrapper=new LambdaQueryWrapper<>();
            queryWrapper.eq(Teachplan::getGrade,1);
            queryWrapper.eq(Teachplan::getCourseId,teachplan.getCourseId());
            Teachplan one = this.getOne(queryWrapper);
            teachplan.setParentid(one.getId());
        }
        //如果是新增第三级章节 则修改课程对应的章节数
        if (teachplan.getId()==null&&teachplan.getGrade()==3){
            courseBase.setPlanNum(courseBase.getPlanNum()+1);
            courseBaseService.updateById(courseBase);
        }
        this.saveOrUpdate(teachplan);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult deleteTeachplan(Long id) {
        Teachplan teachplan = this.getById(id);
        CourseBase courseBase = courseBaseService.getById(teachplan.getCourseId());
        //判断状态是否为可修改
        String auditStatus = courseBase.getAuditStatus();
        if (StrUtil.equals(auditStatus, CourseConstants.AUDIT_COMMIT_STATUS) ||
                StrUtil.equals(auditStatus, CourseConstants.AUDIT_PASTED_STATUS) ||
                StrUtil.equals(auditStatus, CourseConstants.AUDIT_PUBLISHED_STATUS)) {
            throw new BusinessException(ErrorCode.DELETEERROR);
        }
        //如果是二级章节 则查询分级下是否有三级章节
        if (teachplan.getGrade()==2){
            LambdaQueryWrapper<Teachplan>queryWrapper=new LambdaQueryWrapper<>();
            queryWrapper.eq(Teachplan::getParentid,id);
            List<Teachplan> list = this.list(queryWrapper);
            if (list.size()>0){
                throw new BusinessException(ErrorCode.DELETEERROR);
            }
        }else {
            //如果是三级章节 直接删除，并修改对应的课程章节数
            courseBase.setPlanNum(courseBase.getPlanNum()-1);
            courseBaseService.updateById(courseBase);
        }
        this.removeById(id);
        return ResponseResult.okResult();
    }

}
